package com.alonsoruibal.chess.bitboard;

public class GenerateBishopMagic {
	
	public static final long b_d = 0x00000000000000ffL; // down
	private static final long b_u = 0xff00000000000000L; // up
	private static final long b_r = 0x0101010101010101L; // right
	private static final long b_l = 0x8080808080808080L; // left

	private static long getBishopShiftAttacks(long square, long all) {
		return Gen.checkSquareAttackedAux(square, all, +9, b_u | b_l) |
			Gen.checkSquareAttackedAux(square, all, +7, b_u | b_r) |
			Gen.checkSquareAttackedAux(square, all, -7, b_d | b_l) |
			Gen.checkSquareAttackedAux(square, all, -9, b_d | b_r);
	}
	static void generateAttacks() {
	
		BishopMagic.bishopMask = new long[64];
		BishopMagic.bishopMagic = new long[64][];
	
		long square = 1;
		byte i = 0;
		while (square != 0) {
			
			BishopMagic.bishopMask[i] = Gen.squareAttackedAuxSliderMask(square, +9, b_u | b_l) 
					| Gen.squareAttackedAuxSliderMask(square, +7, b_u | b_r)
					| Gen.squareAttackedAuxSliderMask(square, -7, b_d | b_l)
					| Gen.squareAttackedAuxSliderMask(square, -9, b_d | b_r);

			int bishopPositions = (1 << BishopMagic.bishopShiftBits[i]);
			BishopMagic.bishopMagic[i] = new long[bishopPositions];
		    for (int j = 0; j < bishopPositions; j++) {
		      long pieces = Gen.generatePieces(j, BishopMagic.bishopShiftBits[i], BishopMagic.bishopMask[i]);
		      int magicIndex = Gen.magicTransform(pieces, BishopMagic.bishopMagicNumber[i], BishopMagic.bishopShiftBits[i]);
		      BishopMagic.bishopMagic[i][magicIndex] = getBishopShiftAttacks(square, pieces);
		    }
		    
			square <<= 1;
			i++;
		}
	}
}
